clear all
set more off
set matsize 10000
set maxvar 10000
adopath + ../code/gslab_tools/


****************************
* 1996 - 1999 Loop
*********************

local weight96 WTDPER96
local weight97 WTDPER97
local weight98 WTDPER98
local weight99 PERWT99F

foreach year in 96 97 98 99 {
	use DUPERSID AGE`year'X SEX RACEX HISPANX `weight`year'' using ///
		../derived/MEPS/MEPS_consolidated_19`year'.dta, clear
	rename AGE`year'X age
	rename `weight`year'' weight
	rename SEX sex
	rename RACEX race
	rename HISPANX hispanic

	replace age =. if age ==-1
	merge 1:m DUPERSID using ../derived/MEPS/MEPS_prescription_19`year'.dta, ///
		assert(1 3) keepusing(RXNAME RXNDC) nogen
		
	rename DUPERSID person_id
	rename RXNAME rxname
	rename RXNDC rxndc
	
	gen year=1900+`year'
	save ../derived/MEPS/meps_merged_19`year'.dta, replace
}

****************************
* 2000 - 2015 Loop
*********************

local consolidated00 h50
local prescription00 h51a

local consolidated01 h60
local prescription01 h59a

local consolidated02 h70
local prescription02 h67a

local consolidated03 h79
local prescription03 h77a

local consolidated04 h89
local prescription04 h85a

local consolidated05 h97
local prescription05 h94a

local consolidated06 h105
local prescription06 h102a

local consolidated07 h113
local prescription07 h110a

local consolidated08 h121
local prescription08 h118a

local consolidated09 h129
local prescription09 h126a

local consolidated10 h138
local prescription10 h135a

local consolidated11 h147
local prescription11 h144a

local consolidated12 h155
local prescription12 h152a

local consolidated13 h163
local prescription13 h160a

local consolidated14 h171
local prescription14 h168a

local consolidated15 h181
local prescription15 h178a

foreach year in 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 {
	if !inlist("`year'", "12", "13", "14", "15"){
		use dupersid age`year'x sex racex hispanx perwt`year'f using ///
		../raw/MEPS/20`year'/`consolidated`year''.dta, clear	
	}
	else{
		use dupersid age`year'x sex racev1x hispanx perwt`year'f using ///
		../raw/MEPS/20`year'/`consolidated`year''.dta, clear		
	}

	rename age`year'x age
	rename perwt`year'f weight
	rename race* race
	rename hispanx hispanic

	replace age =. if age ==-1
	merge 1:m dupersid using ../raw/MEPS/20`year'/`prescription`year''.dta, ///
		assert(1 3) keepusing(rxname rxndc) nogen
		
	rename dupersid person_id

	gen year=2000+`year'
	save ../derived/MEPS/meps_merged_20`year'.dta, replace
}

//****
* 2016 
*****

use DUPERSID AGELAST SEX RACEV1X HISPANX PERWT16F using ///
	../derived/MEPS/MEPS_consolidated_2016.dta, clear
rename AGELAST age
rename SEX sex
rename RACEV1X race
rename HISPANX hispanic
rename PERWT16F weight

replace age =. if age ==-1
merge 1:m DUPERSID using ../derived/MEPS/MEPS_prescription_2016.dta, ///
	assert(1 3) keepusing(RXNAME) nogen

rename DUPERSID person_id
rename RXNAME rxname

gen year=2016
save ../derived/MEPS/meps_merged_2016.dta, replace

//****
* 2017
*****

use DUPERSID AGELAST SEX RACEV1X HISPANX PERWT17F using ///
	../raw/MEPS/2017/h201.dta, clear
rename AGELAST age
rename SEX sex
rename RACEV1X race
rename HISPANX hispanic
rename PERWT17F weight

replace age =. if age ==-1
merge 1:m DUPERSID using ../raw/MEPS/2017/h197a.dta, ///
	assert(1 3) keepusing(RXNAME) nogen

rename DUPERSID person_id
rename RXNAME rxname

gen year=2017
save ../derived/MEPS/meps_merged_2017.dta, replace


//****
* 2018
*****

use DUPERSID AGELAST SEX RACEV1X HISPANX PERWT18F using ///
	../raw/MEPS/2018/h209, clear
rename AGELAST age
rename SEX sex
rename RACEV1X race
rename HISPANX hispanic
rename PERWT18F weight

replace age =. if age ==-1
merge 1:m DUPERSID using ../raw/MEPS/2018/h206a.dta, ///
	assert(1 3) keepusing(RXNAME) nogen

rename DUPERSID person_id
rename RXNAME rxname

gen year=2018
save ../derived/MEPS/meps_merged_2018.dta, replace

//****
* 2019
*****

use DUPERSID AGELAST SEX RACEV1X HISPANX PERWT19F using ///
	../raw/MEPS/2019/h216.dta, clear
rename AGELAST age
rename SEX sex
rename RACEV1X race
rename HISPANX hispanic
rename PERWT19F weight

replace age =. if age ==-1
merge 1:m DUPERSID using ../raw/MEPS/2019/h213a.dta, ///
	assert(1 3) keepusing(RXNAME) nogen

rename DUPERSID person_id
rename RXNAME rxname

gen year=2019
save ../derived/MEPS/meps_merged_2019.dta, replace



****
* Combine Years
****
use ../derived/MEPS/meps_merged_1996.dta, clear
forvalues year = 1997/2019 {
	append using ../derived/MEPS/meps_merged_`year'.dta
}
save ../derived/MEPS/meps_1996_2019.dta, replace



** Clean up files
forvalues year = 1996/2019 {
	cap erase  ../derived/MEPS/meps_merged_`year'.dta
}
forvalues year = 1996/1999 {
	cap erase  ../derived/MEPS/meps_consolidated_`year'.dta
	cap erase  ../derived/MEPS/meps_prescription_`year'.dta
}
cap erase  ../derived/MEPS/meps_consolidated_2016.dta
cap erase  ../derived/MEPS/meps_prescription_2016.dta